package com.weilan.blog.admin.controller;


import com.weilan.blog.admin.service.BlogCategoryService;
import com.weilan.blog.admin.vo.BlogCategoryVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.MessageConf;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/9/23 15:37
 */
@Slf4j
@RestController

@RequestMapping("/blogCategory")
public class BlogCategoryController {
    @Autowired
    private BlogCategoryService blogCategoryService;


    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody BlogCategoryVO blogCategoryVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取博客分类列表");
        return ResultUtil.result(SysConf.SUCCESS, blogCategoryService.getPageList(blogCategoryVO));
    }

    /**
     * 新增
     *
     * @param blogCategoryVO
     * @param result
     * @return
     */
    @PostMapping("/add")
    @AvoidRepeatableCommit
    public String add(@Validated({Insert.class}) @RequestBody BlogCategoryVO blogCategoryVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);

        return blogCategoryService.addBlogCategory(blogCategoryVO);
    }

    /**
     * 修改
     *
     * @param blogCategoryVO
     * @param result
     * @return
     */
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody BlogCategoryVO blogCategoryVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogCategoryService.editBlogCategory(blogCategoryVO);
    }

    @DeleteMapping("/{id}")
    public String deleteById(@Validated({Delete.class}) @PathVariable("id") String id) {
        // 参数校验
        boolean save = blogCategoryService.removeById(id);
        if (save) {

            return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
        } else {
            return ResultUtil.result(SysConf.ERROR, MessageConf.DELETE_FAIL);
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping ("/deleteBatch/{ids}")
    public String delete(@Validated({Delete.class}) @PathVariable("ids") String [] ids) {



        return blogCategoryService.deleteBatchBlogCategory(ids);
    }

    /**
     * 置顶博客
     *
     * @param blogCategoryVO
     * @param result
     * @return
     */
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody BlogCategoryVO blogCategoryVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogCategoryService.stickBlogCategory(blogCategoryVO);

    }

    /**
     * 查询点击次数哦
     *
     * @return
     */
    @PostMapping("/blogCategoryByClickCount")
    public String blogSortByClickCount() {
        return blogCategoryService.blogCategoryByClickCount();
    }

    /**
     * 根据博客的引用量进行排序
     * @return
     */
    @PostMapping("/blogCategoryByCite")
    public String blogSortByCite() {
        return blogCategoryService.blogCategoryByCite();
    }
}
